/**
 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.omn.listazakupow.db.service.persistence;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import com.omn.listazakupow.db.NoSuchGrupyTowarowException;
import com.omn.listazakupow.db.model.GrupyTowarow;
import com.omn.listazakupow.db.model.impl.GrupyTowarowImpl;
import com.omn.listazakupow.db.model.impl.GrupyTowarowModelImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the grupy towarow service.
 *
 * <p>
 * Never modify or reference this class directly. Always use {@link GrupyTowarowUtil} to access the grupy towarow persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
 * </p>
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see GrupyTowarowPersistence
 * @see GrupyTowarowUtil
 * @generated
 */
public class GrupyTowarowPersistenceImpl extends BasePersistenceImpl<GrupyTowarow>
	implements GrupyTowarowPersistence {
	public static final String FINDER_CLASS_NAME_ENTITY = GrupyTowarowImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
		".List";
	public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(GrupyTowarowModelImpl.ENTITY_CACHE_ENABLED,
			GrupyTowarowModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(GrupyTowarowModelImpl.ENTITY_CACHE_ENABLED,
			GrupyTowarowModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countAll", new String[0]);

	/**
	 * Caches the grupy towarow in the entity cache if it is enabled.
	 *
	 * @param grupyTowarow the grupy towarow to cache
	 */
	public void cacheResult(GrupyTowarow grupyTowarow) {
		EntityCacheUtil.putResult(GrupyTowarowModelImpl.ENTITY_CACHE_ENABLED,
			GrupyTowarowImpl.class, grupyTowarow.getPrimaryKey(), grupyTowarow);
	}

	/**
	 * Caches the grupy towarows in the entity cache if it is enabled.
	 *
	 * @param grupyTowarows the grupy towarows to cache
	 */
	public void cacheResult(List<GrupyTowarow> grupyTowarows) {
		for (GrupyTowarow grupyTowarow : grupyTowarows) {
			if (EntityCacheUtil.getResult(
						GrupyTowarowModelImpl.ENTITY_CACHE_ENABLED,
						GrupyTowarowImpl.class, grupyTowarow.getPrimaryKey(),
						this) == null) {
				cacheResult(grupyTowarow);
			}
		}
	}

	/**
	 * Clears the cache for all grupy towarows.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache() {
		CacheRegistryUtil.clear(GrupyTowarowImpl.class.getName());
		EntityCacheUtil.clearCache(GrupyTowarowImpl.class.getName());
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
	}

	/**
	 * Clears the cache for the grupy towarow.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache(GrupyTowarow grupyTowarow) {
		EntityCacheUtil.removeResult(GrupyTowarowModelImpl.ENTITY_CACHE_ENABLED,
			GrupyTowarowImpl.class, grupyTowarow.getPrimaryKey());
	}

	/**
	 * Creates a new grupy towarow with the primary key. Does not add the grupy towarow to the database.
	 *
	 * @param id the primary key for the new grupy towarow
	 * @return the new grupy towarow
	 */
	public GrupyTowarow create(long id) {
		GrupyTowarow grupyTowarow = new GrupyTowarowImpl();

		grupyTowarow.setNew(true);
		grupyTowarow.setPrimaryKey(id);

		return grupyTowarow;
	}

	/**
	 * Removes the grupy towarow with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the grupy towarow to remove
	 * @return the grupy towarow that was removed
	 * @throws com.liferay.portal.NoSuchModelException if a grupy towarow with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public GrupyTowarow remove(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return remove(((Long)primaryKey).longValue());
	}

	/**
	 * Removes the grupy towarow with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param id the primary key of the grupy towarow to remove
	 * @return the grupy towarow that was removed
	 * @throws com.omn.listazakupow.db.NoSuchGrupyTowarowException if a grupy towarow with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public GrupyTowarow remove(long id)
		throws NoSuchGrupyTowarowException, SystemException {
		Session session = null;

		try {
			session = openSession();

			GrupyTowarow grupyTowarow = (GrupyTowarow)session.get(GrupyTowarowImpl.class,
					new Long(id));

			if (grupyTowarow == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + id);
				}

				throw new NoSuchGrupyTowarowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					id);
			}

			return remove(grupyTowarow);
		}
		catch (NoSuchGrupyTowarowException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected GrupyTowarow removeImpl(GrupyTowarow grupyTowarow)
		throws SystemException {
		grupyTowarow = toUnwrappedModel(grupyTowarow);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, grupyTowarow);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.removeResult(GrupyTowarowModelImpl.ENTITY_CACHE_ENABLED,
			GrupyTowarowImpl.class, grupyTowarow.getPrimaryKey());

		return grupyTowarow;
	}

	public GrupyTowarow updateImpl(
		com.omn.listazakupow.db.model.GrupyTowarow grupyTowarow, boolean merge)
		throws SystemException {
		grupyTowarow = toUnwrappedModel(grupyTowarow);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, grupyTowarow, merge);

			grupyTowarow.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.putResult(GrupyTowarowModelImpl.ENTITY_CACHE_ENABLED,
			GrupyTowarowImpl.class, grupyTowarow.getPrimaryKey(), grupyTowarow);

		return grupyTowarow;
	}

	protected GrupyTowarow toUnwrappedModel(GrupyTowarow grupyTowarow) {
		if (grupyTowarow instanceof GrupyTowarowImpl) {
			return grupyTowarow;
		}

		GrupyTowarowImpl grupyTowarowImpl = new GrupyTowarowImpl();

		grupyTowarowImpl.setNew(grupyTowarow.isNew());
		grupyTowarowImpl.setPrimaryKey(grupyTowarow.getPrimaryKey());

		grupyTowarowImpl.setId(grupyTowarow.getId());
		grupyTowarowImpl.setUserId(grupyTowarow.getUserId());
		grupyTowarowImpl.setKolejnosc(grupyTowarow.getKolejnosc());
		grupyTowarowImpl.setOpis(grupyTowarow.getOpis());

		return grupyTowarowImpl;
	}

	/**
	 * Finds the grupy towarow with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the grupy towarow to find
	 * @return the grupy towarow
	 * @throws com.liferay.portal.NoSuchModelException if a grupy towarow with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public GrupyTowarow findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the grupy towarow with the primary key or throws a {@link com.omn.listazakupow.db.NoSuchGrupyTowarowException} if it could not be found.
	 *
	 * @param id the primary key of the grupy towarow to find
	 * @return the grupy towarow
	 * @throws com.omn.listazakupow.db.NoSuchGrupyTowarowException if a grupy towarow with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public GrupyTowarow findByPrimaryKey(long id)
		throws NoSuchGrupyTowarowException, SystemException {
		GrupyTowarow grupyTowarow = fetchByPrimaryKey(id);

		if (grupyTowarow == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + id);
			}

			throw new NoSuchGrupyTowarowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				id);
		}

		return grupyTowarow;
	}

	/**
	 * Finds the grupy towarow with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the grupy towarow to find
	 * @return the grupy towarow, or <code>null</code> if a grupy towarow with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public GrupyTowarow fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the grupy towarow with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param id the primary key of the grupy towarow to find
	 * @return the grupy towarow, or <code>null</code> if a grupy towarow with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public GrupyTowarow fetchByPrimaryKey(long id) throws SystemException {
		GrupyTowarow grupyTowarow = (GrupyTowarow)EntityCacheUtil.getResult(GrupyTowarowModelImpl.ENTITY_CACHE_ENABLED,
				GrupyTowarowImpl.class, id, this);

		if (grupyTowarow == null) {
			Session session = null;

			try {
				session = openSession();

				grupyTowarow = (GrupyTowarow)session.get(GrupyTowarowImpl.class,
						new Long(id));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (grupyTowarow != null) {
					cacheResult(grupyTowarow);
				}

				closeSession(session);
			}
		}

		return grupyTowarow;
	}

	/**
	 * Finds all the grupy towarows.
	 *
	 * @return the grupy towarows
	 * @throws SystemException if a system exception occurred
	 */
	public List<GrupyTowarow> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the grupy towarows.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of grupy towarows to return
	 * @param end the upper bound of the range of grupy towarows to return (not inclusive)
	 * @return the range of grupy towarows
	 * @throws SystemException if a system exception occurred
	 */
	public List<GrupyTowarow> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Finds an ordered range of all the grupy towarows.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of grupy towarows to return
	 * @param end the upper bound of the range of grupy towarows to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of grupy towarows
	 * @throws SystemException if a system exception occurred
	 */
	public List<GrupyTowarow> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<GrupyTowarow> list = (List<GrupyTowarow>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_GRUPYTOWAROW);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_GRUPYTOWAROW;
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<GrupyTowarow>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<GrupyTowarow>)QueryUtil.list(q, getDialect(),
							start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
						finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
						list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the grupy towarows from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (GrupyTowarow grupyTowarow : findAll()) {
			remove(grupyTowarow);
		}
	}

	/**
	 * Counts all the grupy towarows.
	 *
	 * @return the number of grupy towarows
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Object[] finderArgs = new Object[0];

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_GRUPYTOWAROW);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the grupy towarow persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.omn.listazakupow.db.model.GrupyTowarow")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<GrupyTowarow>> listenersList = new ArrayList<ModelListener<GrupyTowarow>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<GrupyTowarow>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	public void destroy() {
		EntityCacheUtil.removeCache(GrupyTowarowImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
	}

	@BeanReference(type = ListaZakupowPersistence.class)
	protected ListaZakupowPersistence listaZakupowPersistence;
	@BeanReference(type = PozycjeListyZakupowPersistence.class)
	protected PozycjeListyZakupowPersistence pozycjeListyZakupowPersistence;
	@BeanReference(type = GrupyTowarowPersistence.class)
	protected GrupyTowarowPersistence grupyTowarowPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_GRUPYTOWAROW = "SELECT grupyTowarow FROM GrupyTowarow grupyTowarow";
	private static final String _SQL_COUNT_GRUPYTOWAROW = "SELECT COUNT(grupyTowarow) FROM GrupyTowarow grupyTowarow";
	private static final String _ORDER_BY_ENTITY_ALIAS = "grupyTowarow.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No GrupyTowarow exists with the primary key ";
	private static Log _log = LogFactoryUtil.getLog(GrupyTowarowPersistenceImpl.class);
}